package com.xbongbong.saas.model.impl;

import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.enums.DelEnum;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.DateTimeUtil;
import com.xbongbong.saas.constant.ParameterConstant;
import com.xbongbong.saas.constant.StringConstant;
import com.xbongbong.saas.domain.dao.SupplierProductDao;
import com.xbongbong.saas.domain.entity.SupplierProductEntity;
import com.xbongbong.saas.model.SupplierProductModel;
import com.xbongbong.saas.util.StandardizeDataUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author WangJian
 * @date 2019-01-25 15:38
 **/
@Service("SupplierProductModel")
public class SupplierProductModelImpl implements SupplierProductModel {
    @Resource
    private SupplierProductDao supplierProductDao;

    /**
     * 插入
     *
     * @param entity 实体
     * @return 是否成功
     * @throws XbbException 数据库操作异常
     */
    @Override
    public Integer insert(SupplierProductEntity entity) throws XbbException {
        Long now = DateTimeUtil.getInt();
        entity.setAddTime(now);
        entity.setUpdateTime(now);
        StandardizeDataUtil.standardizeNotString(entity);
        return supplierProductDao.insert(entity);
    }

    /**
     * 批量插入
     *
     * @param list 实体
     * @return 是否成功
     */
    @Override
    public Integer batchInsert (List<SupplierProductEntity> list) throws XbbException {
        StandardizeDataUtil.standardize4SupplierProductEntityList(list);
        return supplierProductDao.batchInsert(list);
    }

    @Override
    public Integer updateBatch(List<SupplierProductEntity> list, String corpid) throws XbbException {
        StandardizeDataUtil.standardize4SupplierProductEntityList(list);
        return supplierProductDao.updateBatch(list, corpid);
    }

    @Override
    public Integer update(SupplierProductEntity entity) throws XbbException {
        Long now = DateTimeUtil.getInt();
        entity.setUpdateTime(now);
        StandardizeDataUtil.standardizeNotString(entity);
        return supplierProductDao.update(entity);
    }

    /**
     * 获取符合查询条件的记录
     *
     * @param param 查询条件
     * @return 符合查询条件的记录
     */
    @Override
    public List<SupplierProductEntity> findEntitys(Map<String, Object> param) {
        return supplierProductDao.findEntitys(param);
    }

    /**
     * 获取供应商产品
     *
     * @param corpid     公司ID
     * @param supplierId 供应商ID
     * @return List<SupplierProductEntity>
     */
    @Override
    public List<SupplierProductEntity> getSupplierProductBySupplierId(String corpid, Long supplierId) {
        Map<String, Object> param = new HashMap<>(16);
        param.put("corpid", corpid);
        param.put("del", 0);
        param.put("supplierId", supplierId);
        // 供应商产品筛掉父产品
        param.put("parentIdNot",0);
        param.put(ParameterConstant.ORDER_BY_STR, StringConstant.SAAS_LINK_BUSINESS_ID);
        return findEntitys(param);
    }

    @Override
    public List<SupplierProductEntity> getSupplierProductByProductId(String corpid, Long productId) {
        Map<String, Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        param.put(ParameterConstant.CORPID, corpid);
        param.put(ParameterConstant.DEL, DelEnum.NORMAL.getDel());
        param.put(ParameterConstant.PRODUCT_ID, productId);
        return findEntitys(param);
    }

    @Override
    public List<SupplierProductEntity> getSupplierProductByParentId(String corpid, Long parentId) {
        Map<String, Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        param.put(ParameterConstant.CORPID, corpid);
        param.put(ParameterConstant.DEL, DelEnum.NORMAL.getDel());
        param.put(ParameterConstant.PARENT_ID, parentId);
        return findEntitys(param);
    }

    @Override
    public Map<Long, List<SupplierProductEntity>> getSupplierProductByParentIdIn(String corpid, List<Long> parentIdIn, boolean isSpuList) {
        Map<String, Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        param.put(ParameterConstant.CORPID, corpid);
        param.put(ParameterConstant.DEL, DelEnum.NORMAL.getDel());
        if (isSpuList){
            param.put("parentIdIn", parentIdIn);
        }else {
            param.put("productIdIn", parentIdIn);
        }
        List<SupplierProductEntity> entitys = findEntitys(param);
        Map<Long, List<SupplierProductEntity>> map = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        for (SupplierProductEntity supplierProductEntity : entitys) {
            List<SupplierProductEntity> list = new ArrayList<>();
            Long dataId;
            if (isSpuList){
                dataId = supplierProductEntity.getParentId();
            }else {
                dataId = supplierProductEntity.getProductId();
            }
            if (map.containsKey(dataId))  {
                list = map.get(dataId);
            }
            list.add(supplierProductEntity);
            map.put(dataId, list);
        }
        return map;
    }

    @Override
    public List<SupplierProductEntity> getSupplierProductBySupplierIdIn(String corpid, List<Long> supplierIdIn) {
        Map<String, Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        param.put(ParameterConstant.CORPID, corpid);
        param.put(ParameterConstant.DEL, DelEnum.NORMAL.getDel());
        param.put("supplierIdIn", supplierIdIn);
        return findEntitys(param);
    }

    @Override
    public Integer deleteByParentIdAndSupplierIdIn(Long parentId, List<Long> supplierIdIn, String corpid) {
        return supplierProductDao.deleteByParentIdAndSupplierIdIn(parentId, supplierIdIn, corpid);
    }

    @Override
    public Integer deleteBySupplierIdAndParentIdIn(Long supplierId, List<Long> parentIdIn, String corpid) {
        return supplierProductDao.deleteBySupplierIdAndParentIdIn(supplierId, parentIdIn, corpid);
    }

    /**
     * 获取符合查询条件的记录总数
     *
     * @param param 查询条件
     * @return 符合查询条件的记录总数
     */
    @Override
    public Integer getEntitysCount(Map<String, Object> param) {
        return supplierProductDao.getEntitysCount(param);
    }

    @Override
    public List findEntitysWithoutSub(Map<String, Object> param) {
        return null;
    }

    @Override
    public Integer deleteByKey(Long key, String corpid) {
        return supplierProductDao.deleteByKey(key, corpid);
    }

    @Override
    public Integer deleteByKeys(List<Long> keys, String corpid) {
        return supplierProductDao.deleteByKeys(keys, corpid);
    }

    @Override
    public SupplierProductEntity getByKey(Long key, String corpid) {
        return supplierProductDao.getByKey(key, corpid);
    }

    @Override
    public Integer deleteByProductId(Long productId, String corpid) {
        return supplierProductDao.deleteByProductId(productId, corpid);
    }

    @Override
    public Integer deleteBySupplierId(Long supplierId, String corpid) {
        return supplierProductDao.deleteBySupplierId(supplierId, corpid);
    }

    @Override
    public Integer deleteBySupplierIdIn(String corpid, List<Long> supplierIdIn) {
        return supplierProductDao.deleteBySupplierIdIn(corpid, supplierIdIn);
    }

    /**
     * 保存
     *
     * @param supplierProductEntity 实体
     */
    @Override
    public Integer save(SupplierProductEntity supplierProductEntity) throws XbbException {
        Long id = supplierProductEntity.getId();
        if (id == null || id == 0L) {
            return insert(supplierProductEntity);
        } else {
            return update(supplierProductEntity);
        }
    }

    @Override
    public Integer dataConsistencyUpdateSupplier(String corpid, Long dataId, String name) {
        return supplierProductDao.dataConsistencyUpdate(corpid, dataId, name);
    }

    @Override
    public Long isProductSubAttrExist(String attr, String corpid, Integer isArray) {
        return supplierProductDao.isProductSubAttrExist(attr, corpid ,isArray);
    }

    @Override
    public Integer delBySupplierIdAndId(Long supplierId, Long productSubId, String corpid) {
        return supplierProductDao.delBySupplierIdAndId(supplierId, productSubId, corpid);
    }

    @Override
    public Integer deleteByProductIdIn(List<Long> productIdIn, String corpid) {
        if (CollectionUtils.isNotEmpty(productIdIn)) {
            return supplierProductDao.deleteByProductIdIn(productIdIn, corpid);
        }
        return null;
    }

    @Override
    public Double summaryByAttr(String field,String corpid,  Long refId, List<Long> productIdIn){
        return supplierProductDao.summaryByAttr(field, corpid, refId, productIdIn);
    }

    @Override
    public Long countProductSub(String attr, String corpid, Integer isArray) {
        return supplierProductDao.countProductSub(attr, corpid, isArray);
    }

    @Override
    public List<SupplierProductEntity> getProductWithSubData(String attr, String corpid, Integer isArray, Long idGte, Integer limit) {
        return supplierProductDao.getProductWithSubData(attr, corpid, isArray, idGte, limit);
    }

    @Override
    public List<SupplierProductEntity> findCommonSupplier(String corpid, List<Long> productIds) {
        return supplierProductDao.findCommonSupplier(corpid,productIds);
    }
}
